package com.example.service.appservice.impl;

import com.example.config.RedisUtil;
import com.example.dto.PostsDTO;
import com.example.mapper.appmapper.AppForumMapper;
import com.example.result.CommonResult;
import com.example.service.appservice.AppForumService;
import com.example.util.RandomUtil;
import com.example.vo.CommentVO;
import com.example.vo.PostsVO;
import com.example.vo.RentalVO;
import com.example.vo.ReportVO;
import lombok.extern.log4j.Log4j2;
import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject："smart-property"
 * @BelongsPackage："com.example.service.appservice.impl"
 * @CreateTime："2022-11-17 13:39"
 * @Description：前端论坛实现类
 * @Author：LK
 */
@Log4j2
@Service
public class AppForumServiceImpl implements AppForumService {

    @Autowired
    private AppForumMapper appForumMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * @description:全查帖子相关信息
     * @author: lk
     * @date: 2022/11/17 13:52
     * @return: com.example.result.CommonResult
     **/
    @Override
    public void postsRedisCheck() {
        try{
            // 查询所有帖子信息
            List<PostsDTO> postsDTOS = appForumMapper.postsCheck();
            redisUtil.set("postsRedisCheck",postsDTOS);
        }catch (Exception e){
            // 查询失败返回null
            redisUtil.set("postsRedisCheck",null);
            log.info("全查帖子相关信息错误原因：" + e);
        }
    }

    /**
     * @description:把浏览量持久化到数据库
     * @author: lk
     * @date: 2022/11/18 9:51
     * @param: postsNumber
     * @param: browseCount
     **/
    @Override
    public void appBrowseCount(String postsNumber, Integer browseCount) {
        try {
            appForumMapper.appBrowseCount(postsNumber,browseCount);
        }catch (Exception e){
            log.info("持久化任务报错信息：" + e);
        }
    }

    /**
     * @description:发送评论
     * @author: lk
     * @date: 2022/11/20 20:57
     * @param: commentVO
     * @return: com.example.result.CommonResult
     **/
    @Override
    public CommonResult sendComments(CommentVO commentVO) {
        try {
            // 生成编号
            String str = "PL";
            commentVO.setCommentNumber(str + RandomUtil.getSixBitRandom());
            appForumMapper.sendComments(commentVO);
            return CommonResult.ok();
        }catch (Exception e){
            log.info("发送评论失败原因:" + e);
            return CommonResult.fail();
        }
    }

    /**
     * @description:举报帖子
     * @author: lk
     * @date: 2022/11/21 20:10
     * @param: reportVO
     * @return: com.example.result.CommonResult
     **/
    @Override
    public CommonResult report1(ReportVO reportVO) {
        try {
            reportVO.setReportNumber("JB" + RandomUtil.getSixBitRandom());
            appForumMapper.report1(reportVO);
            return CommonResult.ok();
        }catch (Exception e){
            log.info("举报提交错误原因：" + e);
            return CommonResult.fail();
        }
    }

    /**
     * @description:查询父评论
     * @author: lk
     * @date: 2022/11/22 10:24
     * @return: com.example.result.CommonResult
     **/
    @Override
    public CommonResult commentFather(String postsNumber) {
        try {
            List<CommentVO> commentVOS = appForumMapper.commentFather(postsNumber);
            return CommonResult.ok(commentVOS);
        }catch (Exception e){
            log.info("查询父评论错误原因：" + e);
            return CommonResult.fail();
        }
    }

    /**
     * @description:发布帖子
     * @author: lk
     * @date: 2022/11/22 15:48
     * @param: postsVO
     * @return: com.example.result.CommonResult
     **/
    @Override
    public CommonResult release(PostsVO postsVO) {
        try{
            postsVO.setPostsNumber("TZ" + RandomUtil.getSixBitRandom());
            appForumMapper.release(postsVO);
            return CommonResult.ok();
        }catch (Exception e){
            log.info("发布帖子失败原因：" + e);
            return CommonResult.fail();
        }
    }

    /**
     * @description:查询房子信息
     * @author: lk
     * @date: 2022/11/23 10:17
     * @return: com.example.result.CommonResult
     **/
    @Override
    public CommonResult checkRabtal() {
        try {
            return CommonResult.ok(appForumMapper.checkRabtal());
        }catch (Exception e){
            log.info("查询房子信息错误原因：" + e);
            return CommonResult.fail();
        }
    }

    /**
     * @description:发布租房
     * @author: lk
     * @date: 2022/11/24 9:47
     * @param: rentalVO
     * @return: com.example.result.CommonResult
     **/
    @Override
    public CommonResult fabuRental(RentalVO rentalVO) {
        try {
            appForumMapper.fabuRental(rentalVO);
            return CommonResult.ok();
        } catch (Exception e){
            log.info("发布租房错误原因" + e);
            return CommonResult.fail();
        }
    }

    /**
     * @description:全查房屋
     * @author: lk
     * @date: 2022/11/24 10:51
     * @return: com.example.result.CommonResult
     **/
    @Override
    public CommonResult floorAll() {
        try {
            return CommonResult.ok(appForumMapper.floorAll());
        } catch (Exception e){
            log.info("全查房屋失败原因：" + e);
            return CommonResult.fail();
        }
    }
}
